﻿import flash.external.*;

_global.nc = new NetConnection();

/**
 * @des 记录与会ID，用来取消客户端的通知提示
 */
_global.meetingIdList = "";

nc.connect("rtmp://" + serverIp + "/videoChat", memberId, memberName, 1);

nc.onStatus = function(info:Object) {
	if (info.code == "NetConnection.Connect.Success") {
		/**
		 * @des 獲取當前用戶所參與的所有會議列表
		 */
		var so:SharedObject = SharedObject.getRemote("notify", nc.uri, true);
		so.connect(nc);
		
		so.onSync = function() {
			var debug:String = "";
			var meetingContent:String = "";
			
			for (var i in so.data) {
				debug += so.data[i].memberList + "..." + memberName + "..." + so.data[i].memberList.indexOf(memberName + ":") + "<br>";
				if (so.data[i].memberList.indexOf(memberName + ":") > -1) {
					meetingIdList += so.data[i].id + ",";
					meetingContent += "会议名称：" + so.data[i].meetingName + "<br>" +
									  "会议主题：" + so.data[i].meetingTheme + "<br>" +
									  "创建人：" + so.data[i].createUser + "<br>" +
									  "创建时间：" + swapTime(so.data[i].createTime) + "<br>" +
									  "备注：" + so.data[i].notifyContent + "<br>" +
									  "<br>";
				}
			}
			
			if (meetingContent.length > 0) {
				ExternalInterface.call("popupMsg", debug, meetingContent);
				ExternalInterface.addCallback("cancelNotify",
											  null,
											  function(){
												  nc.call("cancelNotify",
														  null,
														  meetingIdList,
														  memberName,
														  memberId
														  );
											  });
			}
		};
	} else {
		alert("系统提示：", "连接视频服务器失败！", 1);
	}
};

/**
 * @des 更新人员列表
 * @param receiverId 对方ID
 */
nc.updateState = function(receiverId) {
	var tmpArray:Array = memberIdList.split(",");
	
	
	for (var i:Number = 0; i < tmpArray.length; i++) {
		if (tmpArray[i] == receiverId) {
			var o = eval("sp.content.stat" + i);
			var x1 = o._x;
			var y1 = o._y;
			var state = eval("sp.content.un" + i).state;
			var mcName:String = "";
			
			switch (state) {
				case "离线":
					mcName = "offline";
					break;
				case "在线":
					mcName = "online";
					break;
				case "忙碌":
					mcName = "busy";
					break;
			}
			removeMovieClip(o);
			
			nc.call("upLog", null, "结束与 " + receiverName + " 的聊天．．．");
			sp.content.attachMovie(mcName,
								   "stat" + i,
								   sp.content.getNextHighestDepth(),
								   {
									   _x: x1,
									   _y: y1
								   });
			eval("sp.content.un" + i).state = state;
		}
	}
};

/**
 * @des 打開本地的聊天窗口
 */
nc.openWin = function(senderId, senderName, timer) {
	var tmpArray:Array = memberIdList.split(",");
	
	for (var i:Number = 0; i < tmpArray.length; i++) {
		if (tmpArray[i] == senderId) {
			var o = eval("sp.content.stat" + i);
			var x1 = o._x;
			var y1 = o._y;
			
			removeMovieClip(o);
			
			nc.call("upLog", null, "结束与 " + senderName + " 的聊天．．．");
			sp.content.attachMovie("chat",
								   "stat" + i,
								   sp.content.getNextHighestDepth(),
								   {
									   _x: x1,
									   _y: y1
								   });
		}
	}
	
	ExternalInterface.call("openChatWin", senderId, senderName, timer);
};

nc.updateList = function() {
	syncState();
};